home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / PT23MOD.ZIP / MOD-info
Internet Message Format  |  1993-03-25  |  26KB

  1. From: Lars Hamre <larsha@lise.unit.no>
  2. To: guido@cwi.nl
  3. Date: Tue, 23 Mar 1993 19:38:04 +0100
  4.  
  5. There are _NO_ standard sample rate for the samples used in modules.
  6. But most often the samples are done on the rate called C-3 (which is
  7. about 16574 Hz if you have a PAL machine). Sometimes drums are sampled
  8. at A-3 (around 28 kHz), and some sounds are at ~8 kHz or anything else
  9. to save space.
  10.  
  11. The sample rate on each of the channels can be selected by a period value,
  12. which tells the hardware how many ~3.5 MHz clocks to count down before
  13. playing the next sample. If you have a 16 kHz sample you simply play
  14. it at a note that gives you 16 kHz sample rate. If you play it one octave
  15. lower, you get a 8 kHz sample rate (and a double period value).
  16.  
  17. Here are the magic formulas:
  18.  
  19.                 7093789.2
  20. SampleRate = --------------    (For a PAL machine)
  21.                Period * 2
  22.  
  23.                 7159090.5
  24. SampleRate = --------------    (For a NTSC machine)
  25.                Period * 2
  26.  
  27.  
  28. So, the most normal rate is (C-3, period 214):
  29.  
  30.   7093789.2
  31.  ----------- = 16574.27 Hz
  32.    214 * 2
  33.  
  34. (16726.8 Hz if you use the NTSC formula, but i have a PAL machine)
  35.  
  36. The Amiga has four channels with independent sample rates, so there are
  37. no such thing as a common rate for all channels, and there are no mixing
  38. going on.
  39.  
  40. For a list of period values and what notes they are supposed to correspond
  41. to, see the list of period values in the MOD format below.
  42.  
  43. Also note that finetuning are actually done by switching between 16 different
  44. period tables (not included here, but get the Amiga Protracker Playroutine).
  45.  
  46. ---
  47. Lars Hamre
  48. larsha@lise.unit.no
  49.  
  50.  
  51. [ A description of the mod format follows:  -Lars]
  52.  
  53.  
  54. Newsgroups: comp.sys.amiga.audio
  55. From: steinarm@ifi.uio.no (Steinar Midtskogen)
  56. Subject: Old MOD. format + PowerPacking
  57. Summary: Description of the MOD format and powerpacking
  58. Keywords: MOD, Powerpacker
  59. Organization: University of Oslo, Norway
  60. Date: Sun, 14 Mar 1993 23:49:00 GMT
  61.  
  62.   Since everybody seem to ask about the module format, here it comes.  I
  63. will post this information every forthnight, at least for a period (pun
  64. intended).
  65.  
  66.   I have put together most of the documentation about the module format
  67. from PT2.3, edited it heavily (all credit to the original authors, but I
  68. take responsibility for all errors) and added information about
  69. powerpacking and how the Amiga volume work.
  70.  
  71.   The reason for not using documentation following PT3.0, which is written
  72. by my group, is that there is no docmentation on the MOD format following
  73. that.  Our reason for not distributing the documentation of the MOD.
  74. format with the tracker is that we try to discourage the usage of the MOD.
  75. format; a new format is coming up soon.  So don't write a revolutionary
  76. piece of code based on the information presented herein; it will hopefully
  77. be outdated in only another month.
  78.  
  79. [ Outdated? I don't think so... -Lars]
  80.  
  81.   (The "I" in this file refer to Vishnu of Cryptoburners, the "we" refer to
  82. respectively Amiga Freelancers and Cryptoburners; it should be obvious from
  83. the context which of them)
  84.  
  85. Credits for original doc files:
  86.  
  87. Lars "ZAP" Hamre /Amiga Freelancers
  88.   The documentation to the song/module format.
  89.   The information about how to calculate BMPs from the Amiga CIA timings.
  90.  
  91. Peter "CRAYON" Hanning /Mushroom Studios/Noxious
  92.   Updates to the song/module format information. (I thought that it looked
  93.    pretty similar to the one from PT1.3, but he had put his name under,
  94.    so...)
  95.   The table of effect commands.
  96.  
  97. [ Hrm. Both files are mine. I can see no big changes. -Lars]
  98.  
  99.  
  100.   I have updated the information in the song/module format text, and added
  101. information on how the finetuning work.
  102.  
  103. ***********************************************************************
  104.  
  105. Protracker 2.3A Song/Module Format:
  106. -----------------------------------
  107.  
  108. Offset  Bytes  Description
  109. ------  -----  -----------
  110.    0     20    Songname. Remember to put trailing null bytes at the end...
  111.                When written by ProTracker this will be only uppercase;
  112.                there are only historical reasons for this. (And the
  113.                historical reason is that Karsten Obarski, who made the
  114.                first SoundTracker, was stupid.)
  115.  
  116. Information for sample 1-31:
  117.  
  118. Offset  Bytes  Description
  119. ------  -----  -----------
  120.   20     22    Samplename for sample 1. Pad with null bytes. Will only be
  121.                uppercase.  The samplenames are often used for storing
  122.                messages from the author; in particular, samplenames
  123.                starting with a '#' sign will generally be a message.  This
  124.                convention is a result of a player called IntuiTracker
  125.                displaying all samples starting with # as a message to the
  126.                person playing the module.
  127.   42      2    A WORD with samplelength for sample 1.  Stored as number of
  128.                words.  Multiply by two to get real sample length in bytes.
  129.                This is a big-endian number; for all PC programmers out
  130.                there, this means that to get your 8-bit-orginated format,
  131.                you have to swap the two bytes.
  132.   44      1    Lower four bits are the finetune value, stored as a signed
  133.                four bit number. The upper four bits are not used, and
  134.                should be set to zero.
  135.            They should also be masked out reading; you can never be
  136.            sure what some stupid program could have stored here...
  137.   45      1    Volume for sample 1. Range is $00-$40, or 0-64 decimal.
  138.   46      2    Repeat point for sample 1. Stored as number of words offset
  139.                from start of sample. Multiply by two to get offset in bytes.
  140.   48      2    Repeat Length for sample 1. Stored as number of words in
  141.                loop. Multiply by two to get replen in bytes.
  142.  
  143. Information for the next 30 samples starts here. It's just like the info for
  144. sample 1.
  145.  
  146. Offset  Bytes  Description
  147. ------  -----  -----------
  148.   50     30    Sample 2...
  149.   80     30    Sample 3...
  150.    .
  151.    .
  152.    .
  153.  890     30    Sample 30...
  154.  920     30    Sample 31...
  155.  
  156. Offset  Bytes  Description
  157. ------  -----  -----------
  158.  950      1    Songlength. Range is 1-128.
  159.  951      1    This byte is set to 127, so that old trackers will search
  160.                through all patterns when loading.
  161.                Noisetracker uses this byte for restart, ProTracker doesn't.
  162.  952    128    Song positions 0-127.  Each hold a number from 0-63 (or
  163.                0-127) that tells the tracker what pattern to play at that
  164.                position.
  165. 1080      4    The four letters "M.K." - This is something Mahoney & Kaktus
  166.                inserted when they increased the number of samples from
  167.                15 to 31. If it's not there, the module/song uses 15 samples
  168.                or the text has been removed to make the module harder to
  169.                rip. Startrekker puts "FLT4" or "FLT8" there instead.
  170.                If there are more than 64 patterns, PT2.3 will insert M!K!
  171.                here. (Hey - Noxious - why didn't you document the part here
  172.                relating to YOUR OWN PROGRAM? -Vishnu)
  173.  
  174. Offset  Bytes  Description
  175. ------  -----  -----------
  176. 1084    1024   Data for pattern 00.
  177.    .
  178.    .
  179.    .
  180. xxxx  Number of patterns stored is equal to the highest patternnumber
  181.       in the song position table (at offset 952-1079).
  182.  
  183.   Each note is stored as 4 bytes, and all four notes at each position in
  184. the pattern are stored after each other.
  185.  
  186. 00 -  chan1  chan2  chan3  chan4
  187. 01 -  chan1  chan2  chan3  chan4
  188. 02 -  chan1  chan2  chan3  chan4
  189. etc.
  190.  
  191. Info for each note:
  192.  
  193.  _____byte 1_____   byte2_    _____byte 3_____   byte4_
  194. /                \ /      \  /                \ /      \
  195. 0000          0000-00000000  0000          0000-00000000
  196.  
  197. Upper four    12 bits for    Lower four    Effect command.
  198. bits of sam-  note period.   bits of sam-
  199. ple number.                  ple number.
  200.  
  201.   To separate out the different parts of the note, something like this
  202. would be used (C to have it a bit portable; I like assembler and Pascal
  203. better myself):
  204.  
  205. int samplenum,effectcommand,effectdata,extendedcommand;
  206. char notename[];
  207. ...
  208.  
  209. void ProcessNote(byte notedata[]) {
  210.   extendedcommand=-1;
  211.   samplenum=(*notedata&0xF0)|(*(notedata+2)>>4);
  212.   switch(((*notedata<<8)|(*notedata))&0xfff) {
  213.     case 856: notename="C-1"; break;
  214.     case 808: notename="C#1"; break;
  215.     case 762: notename="D-1"; break;
  216.     case 856: notename="D#1"; break;
  217.    /* etc */
  218.     default: notename="???"; /* This should NOT occur; if it do, it is */
  219.                              /* not a ProTracker module! */
  220.    }
  221.    effectcommand=*(notedata+2)&0xF;
  222.    effectdata=*(notedata+3);
  223.    if effectcommand==0xE then /* Extended command */ {
  224.       extendedcommand=effectdata>>4;
  225.       effectdata&=0xf; /* Only one nibble data for extended command */
  226.    }
  227. }
  228.  
  229. Probably this isn't 100% valid C code, but I think you catch my drift...
  230.  
  231.  
  232.  
  233. Periodtable for Tuning 0, Normal
  234.   C-1 to B-1 : 856,808,762,720,678,640,604,570,538,508,480,453
  235.   C-2 to B-2 : 428,404,381,360,339,320,302,285,269,254,240,226
  236.   C-3 to B-3 : 214,202,190,180,170,160,151,143,135,127,120,113
  237.  
  238.   To determine what note to show, scan through the table until you find the
  239. same period as the one stored in byte 1-2.  Use the index to look up in a
  240. notenames table.
  241.  
  242.   If you have a bit of memory, it is probably smarter to use an 744 byte
  243. block to store the indexes relating to the different periods (ie:  at
  244. position 808 in the block you store 1, as 1 is the index of period 808.
  245. Then you use the block as a look up table)
  246.  
  247.   This is the data stored in a normal song.  A packed song starts with the
  248. four letters "PACK", and then comes the packed data.
  249.  
  250.   It is somewhat unclear to me what kind of packing that is referred to
  251. here.  One thing is clear though - it is NOT powerpacked or LHAed modules!
  252. I belive somebody (Probably Amiga Freelancers) was planning to install some
  253. form of direct packing into ProTracker, reserved this ID, and then it never
  254. came...  -Vishnu
  255.  
  256. [No. We never intended to install any packing here. This is an older
  257.  invention :)  Songs (modules WITHOUT the samples) can be packed by this
  258.  method, and it will put the "PACK" tag at the beginning of the file.
  259.  But since everyone saves modules with samples instead of songs, I wouldn't
  260.  care about it. The packer/depacker for PACK'ed files are in the PT source
  261.  code, available on aminet sites.  -Lars Hamre]
  262.  
  263.   In a module, all the samples are stored right after the patterndata.  To
  264. determine where a sample starts and stops, you use the sampleinfo
  265. structures in the beginning of the file (from offset 20).  Take a look at
  266. the mt_init routine in the playroutine, and you'll see just how it is done.
  267.   The data for a sample must _ALWAYS_ start with two zeros, as it is used for
  268. repeating is the sample is to be terminated.
  269.  
  270. [ Well, the playroutine will clear these two bytes anyway...  -Lars]
  271.  
  272. ***********************************************************************
  273.                                 Finetuning
  274.  
  275. Value:    0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
  276. Finetune: 0  +1  +2  +3  +4  +5  +6  +7  -8  -7  -6  -5  -4  -3  -2  -1
  277.  
  278.  
  279.   Finetuning are done by multiplying the frequency of the playback by
  280. X^(finetune), where X ~= 1.0072382087
  281.   This means that Amiga PERIODS, which represent delay times before
  282. fetching the next sample, should be multiplied by X^(-finetune)
  283.  
  284. Vishnu of Cryptoburners
  285.  
  286. [ This should be 2^(finetune/12/8). And 2^(1/12/8) is 1.007246412 on
  287.   my calculator...  (12 notes per octave and 1/8 of this)  -Lars Hamre ]
  288.  
  289.  
  290. ***********************************************************************
  291.           Decibel Values and Volume Ranges
  292.  
  293.    Volume  Decibel Value     Volume  Decibel Value
  294.  
  295.      64         0.0            32        -6.0
  296.      63        -0.1            31        -6.3
  297.      62        -0.3            30        -6.6
  298.      61        -0.4            29        -6.9
  299.      60        -0.6            28        -7.2
  300.      59        -0.7            27        -7.5
  301.      58        -0.9            26        -7.8
  302.      57        -1.0            25        -8.2
  303.      56        -1.2            24        -8.5
  304.      55        -1.3            23        -8.9
  305.      54        -1.5            22        -9.3
  306.      53        -1.6            21        -9.7
  307.      52        -1.8            20       -10.1
  308.      51        -2.0            19       -10.5
  309.      50        -2.1            18       -11.0
  310.      49        -2.3            17       -11.5
  311.      48        -2.5            16       -12.0
  312.      47        -2.7            15       -12.6
  313.      46        -2.9            14       -13.2
  314.      45        -3.1            13       -13.8
  315.      44        -3.3            12       -14.5
  316.      43        -3.5            11       -15.3
  317.      42        -3.7            10       -16.1
  318.      41        -3.9             9       -17.0
  319.      40        -4.1             8       -18.1
  320.      39        -4.3             7       -19.2
  321.      38        -4.5             6       -20.6
  322.      37        -4.8             5       -22.1
  323.      36        -5.0             4       -24.1
  324.      35        -5.2             3       -26.6
  325.      34        -5.5             2       -30.1
  326.      33        -5.8             1       -36.1
  327.                                 0    Minus infinity
  328.  
  329.   The reason for the table starting at 0 dB as the convention from
  330. taperecorders of having 0 dB as the optimal recording condition, and
  331. displaying anything worse as a negative number.
  332.  
  333.   Decibel is a logrithmical unit, just like we feel sound intensity.  It
  334. represent the ratio between two intensities.
  335.  
  336.   On the other hand, the Amiga volumes represent the linear difference
  337. between sound intensities; this mean that you have less accuracy between
  338. the low volumes than between the high ones.  If you need to, you can safely
  339. remove volume 64 and replace it with volume 63; but you can NOT remove
  340. volume 0 and replace it with volume 1.
  341.  
  342.   If you are implementing a MOD player for another sound-device, then
  343. remember to check whether it has linear or logarithmic volume control.
  344.  
  345.   The above table can be calculated from the formula dB=20*log10(Volume/64)
  346. To go the other way, from dB to Amiga volumes, do
  347. Volume=64*10^(dB/20)
  348.  
  349.   The dB here have to do with ratios of sound, not absolute sound power.
  350. This is the way it is used in recording equipment etc, and not the for
  351. measuring absolute ear-destroying capability.
  352.  
  353.   If you need to implement volume artificially, just multiply by the volume
  354. and shift right 6 times.
  355.   If you need to mix samples on-the-fly to lower the amount of voices used,
  356. your best bet is probably doing a DCT (Convert the samples to sums of
  357. cosines) on small blocks of the sample (64 bytes?) before playing, and
  358. mixing with the exact position in which you generate a sample.  This is the
  359. only way I can think of to give adequate quality, at least.
  360.  
  361. -Vishnu of Cryptoburners
  362.  
  363. ***********************************************************************
  364. Protracker V2.3A/3.01 Effect Commands
  365. ----------------------------------------------------------------------------
  366. 0 - Normal play or Arpeggio             0xy : x-first halfnote add, y-second
  367. 1 - Slide Up                            1xx : upspeed
  368. 2 - Slide Down                          2xx : downspeed
  369. 3 - Tone Portamento                     3xx : up/down speed
  370. 4 - Vibrato                             4xy : x-speed,   y-depth
  371. 5 - Tone Portamento + Volume Slide      5xy : x-upspeed, y-downspeed
  372. 6 - Vibrato + Volume Slide              6xy : x-upspeed, y-downspeed
  373. 7 - Tremolo                             7xy : x-speed,   y-depth
  374. 8 - NOT USED
  375. 9 - Set SampleOffset                    9xx : offset (23 -> 2300)
  376. A - VolumeSlide                         Axy : x-upspeed, y-downspeed
  377. B - Position Jump                       Bxx : songposition
  378. C - Set Volume                          Cxx : volume, 00-40
  379. D - Pattern Break                       Dxx : break position in next patt
  380. E - E-Commands                          Exy : see below...
  381. F - Set Speed                           Fxx : speed (00-1F) / tempo (20-FF)
  382. ----------------------------------------------------------------------------
  383. E0- Set Filter                          E0x : 0-filter on, 1-filter off
  384. E1- FineSlide Up                        E1x : value
  385. E2- FineSlide Down                      E2x : value
  386. E3- Glissando Control                   E3x : 0-off, 1-on (use with tonep.)
  387. E4- Set Vibrato Waveform                E4x : 0-sine, 1-ramp down, 2-square
  388. E5- Set Loop                            E5x : set loop point
  389. E6- Jump to Loop                        E6x : jump to loop, play x times
  390. E7- Set Tremolo Waveform                E7x : 0-sine, 1-ramp down. 2-square
  391. E8- NOT USED
  392. E9- Retrig Note                         E9x : retrig from note + x vblanks
  393. EA- Fine VolumeSlide Up                 EAx : add x to volume
  394. EB- Fine VolumeSlide Down               EBx : subtract x from volume
  395. EC- NoteCut                             ECx : cut from note + x vblanks
  396. ED- NoteDelay                           EDx : delay note x vblanks
  397. EE- PatternDelay                        EEx : delay pattern x notes
  398. EF- Invert Loop                         EFx : speed
  399. ----------------------------------------------------------------------------
  400. Peter "CRAYON" Hanning /Mushroom Studios/Noxious
  401.  
  402. For a more complete description see my previous post about the new format.
  403. This format also allow you to have more effects, and several effects on the
  404. same note.  Hopefully, it will soon replace the module format.
  405.  - Vishnu
  406.  
  407. ***********************************************************************
  408.  
  409. Protracker CIA (Complex Interface Adapter) Timer Tempo Calculations:
  410. --------------------------------------------------------------------
  411. Fcolor                        = 4.43361825 MHz (PAL color carrier frequency)
  412. CPU Clock   = Fcolor * 1.6    = 7.0937892  MHz
  413. CIA Clock   = Cpu Clock / 10  = 709.37892  kHz
  414. 50 Hz Timer = CIA Clock / 50  = 14187.5784
  415. Tempo num.  = 50 Hz Timer*125 = 1773447
  416.  
  417. For NTSC: CPU Clock = 7.1590905 MHz --> Tempo num. = 1789773
  418.  
  419.  To calculate tempo we use the formula: TimerValue = 1773447 / Tempo
  420.  The timer is only a word, so the available tempo range is 28-255 (++).
  421.  Tempo 125 will give a normal 50 Hz timer (VBlank).
  422.  
  423.  A normal Protracker VBlank song tempo can be calculated as follows:
  424.  We want to know the tempo in BPM (Beats Per Minute), or rather quarter-
  425.  notes per minute. Four notes makes up a quarternote.
  426.  First find interrupts per minute: 60 seconds * 50 per second = 3000
  427.  Divide by interrupts per quarter note = 4 notes * speed
  428.  This gives: Tempo = 3000/(4*speed)
  429.  simplified: Tempo = 750/speed
  430.  For a normal song in speed 6 this formula gives: 750/6 = 125 BPM
  431.  
  432.  Lars "ZAP" Hamre/Amiga Freelancers 1990
  433.  
  434. ***********************************************************************
  435.  
  436. The "PowerPacker" crunching algorithm:
  437.  
  438. Powerpacker use a variant of Lemel-Ziv compression.  This mean that it in
  439. some cases store strings of bytes as only an offset from the current
  440. position and a counter.  (How LZ could get a patent on this is beyond me!)
  441.  
  442. A PowerPacked file has the following format:
  443.     dc.b    'PP20'            ; Identifier
  444.     dc.l    Efficiency
  445.     ... crunched data ...
  446.     dc.l    (Length*256)+NumOfBitsToDiscard
  447.  
  448. The Efficiency is 4 bytes representing the length of offset from the
  449. current position for different runs of equal bytes.  The first three are
  450. used for runs of from 2 to 4 bytes; the last is used for all runs of 5
  451. bytes and over.
  452. The length is the length of the original, UNcrunched file.
  453. The bits to be discarded are discarded off the END of the crunched data.
  454.  
  455. All bits in the crunched data are stored in reverse order, to permit
  456. decrunching in a buffer where the crunched data are loaded at the start (An
  457. 8 byte margin between the start of the file and the decrunching position is
  458. needed, though).  This mean you have to get bits in the reverse order when
  459. decrunching, and when I refer to "Get A Bit" or Get Eight Bits" or
  460. something, that is the LAST bit or bits from the source.  Kinda obvious
  461. isn't it? ;-)
  462.  
  463. This means that also when a full set of 8 bits are read from the file,
  464. their bit order are reversed.
  465.  
  466. The varying efficiencies used by PowerPacker are as follows:
  467. Fast:      9, 9, 9, 9
  468. Medicore:  9,10,10,10
  469. Good:      9,10,11,11
  470. Very Good: 9,10,12,12
  471. Best:      9,10,12,13
  472.  
  473.  
  474.                          The Decrunching Algorithm
  475.  
  476. WritePointer is a pointer to the position in memory where decrunched bytes
  477.  are currently written.
  478.  
  479. Decrunch:
  480. REPEAT
  481.   Get A Bit (X);
  482.   IF X=0 THEN Copy bytes from source;
  483.   Copy string from already decrunched part of file;
  484.     (* Done no matter what the state of X *)
  485. UNTIL WritePointer<=Start Of Decrunchbuffer;
  486. END.
  487.  
  488.  
  489. Copy bytes from source:
  490. BEGIN
  491.   n:=0;
  492.   REPEAT
  493.     Get Two Bits (X);
  494.     n:=n+X;
  495.   UNTIL X<>3;
  496.   Copy n+1 bytes as bits from Crunched Data to WritePointer;
  497.   (* At this stage, the bytes get their order of bits reversed; and
  498.      WritePointer DECREASES *)
  499. END;
  500.  
  501. Copy string from already decrunched part of file:
  502. BEGIN
  503.   Get Two Bits(n);
  504.   OffsetLen:=Efficiency[n];
  505.   IF n<>3 THEN Get OffsetLen Bits (X)
  506.   ELSE
  507.   BEGIN
  508.     Get One Bit (X);
  509.     IF X=0 THEN Get Seven Bits (X) ELSE Get OffsetLen Bits (X);
  510.     REPEAT
  511.       Get Three Bits (Y);
  512.       n:=n+Y;
  513.     UNTIL Y<>7;
  514.   END;
  515.   Copy n+2 bytes from WritePointer+X;
  516.    (* Here it is copied reversely through memory; X is constant, while
  517.       WritePointer decreases. *)
  518. END;
  519.  
  520. If you can't read this, the original decrunchroutine follows here, in 68000
  521. assembler.  It was NOT commented before I cleared it up with macros...
  522. So complaints about the comments must go to me, too!
  523.  
  524. Registers used are (in the main decrunch routine):
  525. d0 - Counter for number of bits to fetch with the READD1 macro.
  526. d1 - Return register for the READBIT macros.
  527. d2 - Used as counter register for the copy routines.
  528. d3 - Used as offset for the oldstring copy routine.
  529. d5 - Used to store the longword currently read bits from.
  530. d7 - Used for storing the number of bits left in d5.
  531.      0 means one bit left, read new longword to d5 when d7 wraps to
  532.      negative.
  533. a0 - Pointer to current longword of source (the one in d5).
  534. a1 - Current position in the buffer to decrunch to.
  535. a2 - Start of buffer to decrunch to.  Used only for checking whether the
  536.      decrunching is through.
  537. a5 - Pointer to efficiency array.
  538.  
  539. Things to consider:
  540. Bits are shifted from d5 to d1 with code like this
  541.     lsr.l    #1,d5
  542.     addx.l    d1,d1
  543. shifting bits from the BOTTOM of d5 into the BOTTOM of d1, reversing the
  544. order of the bits as they go from d5 to d1.
  545. The predecrement mode, as in
  546.     move.b    d1,-(a1)
  547. decrement a1 BEFORE writing d1.
  548.  
  549. ***********************************************************************
  550. ;
  551. ; PowerPacker Decrunch assembler subroutine V1.1
  552. ;
  553. ; call as:
  554. ;    DecrunchBuffer (endcrun, buffer, efficiency);
  555. ;                      a0       a1        d0
  556. ; with:
  557. ;    endcrun   : UBYTE * just after last byte of crunched file
  558. ;    buffer    : UBYTE * to memory block to decrunch in
  559. ;    efficiency: Longword defining efficiency with wich file was crunched
  560. ;
  561. ; NOTE:
  562. ;    Decrunch a few bytes higher (safety margin) than the crunched file
  563. ;    to decrunch in the same memory space. (64 bytes suffice)
  564. ;
  565.  
  566. Decrunch:
  567.     lea    myBitsTable(pc),a5    ; Efficiency array
  568.     move.l    d0,(a5)            ; Store efficiency for this file.
  569.     move.l    a1,a2            ; Store start of decrunch memory.
  570.     move.l    -(a0),d5        ; Get length & number of bits to 
  571.     moveq    #0,d1            ;  trash...
  572.     move.b    d5,d1            ; Copy number of bits to trash...
  573.     lsr.l    #8,d5            ; Find length of decrunched file...
  574.     add.l    d5,a1            ; And end of decrunch buffer.
  575.     move.l    -(a0),d5        ; First longword of crunched data.
  576.     lsr.l    d1,d5            ; Skip unused bits...
  577.     moveq    #32-1,d7        ; Number of bits in longword...
  578.     sub.b    d1,d7            ; And be sure to read another when 
  579.                     ;  d5 is spent!
  580.  
  581. LoopCheckCrunch:            ; The decrunch loop.
  582.     READBIT                ; State of bit is returned in d1
  583.     bne.s    CrunchedBytes        ; and Z flag
  584. NormalBytes:
  585.     moveq    #0,d2
  586. Read2BitsRow:
  587.     READBITS #2,d1            ; Get length of run-1
  588.     add.w    d1,d2            ; Loop until not %11, increasing
  589.     cmp.w    #3,d1            ; all the time... (Sort of Huffman
  590.     beq.s    Read2BitsRow        ; on the run lengths...)
  591. .ReadNormalByte:            ; REPEAT
  592.     READBITS #8,d1            ; Get from crunched data...
  593.     move.b    d1,-(a1)        ; Store...
  594.     dbf    d2,.ReadNormalByte    ; UNTIL d2<0
  595.     cmp.l    a1,a2            ; End of crunch?
  596.     bcs.s    CrunchedBytes        ; Nope, now do crunched bytes...
  597.     rts                ; Chicken out - FINITO!
  598.  
  599. CrunchedBytes:
  600.     READBITS #2,d1            ; Get 2 bits of runlength-2
  601.     moveq    #0,d0
  602.     move.b    (a5,d1.w),d0        ; Get number of bits offset for
  603.     move.w    d1,d2            ; this runlength
  604.     addq.w    #1,d2            ; Runlength always 2+
  605.     cmp.w    #3+1,d2            ; Did data indicate longer run?
  606.     bne.s    ReadOffset        ; Nope....
  607.     READBIT                ; Is the longer run with offsetlen
  608.     bne.s    .LongBlockOffset    ; from Efficiency?
  609.     moveq    #7,d0            ; Nope, hard code length 7
  610. .LongBlockOffset:
  611.     READD1                ; Get offset...
  612.     move.w    d1,d3
  613. Read3BitsRow:
  614.     READBITS #3,d1            ; Get more string length...
  615.     add.w    d1,d2            ; Increase until stop indicated...
  616.     cmp.w    #7,d1            ; By not having the max value.
  617.     beq.s    Read3BitsRow
  618.     bra.s    DecrunchBlock        ; And start the copying.
  619. ReadOffset:
  620.     READD1                ; Get offset for short run...
  621.     move.w    d1,d3            ; and use it!
  622. DecrunchBlock:
  623.     move.b    (a1,d3.w),-(a1)        ; Loop the copy...
  624.     dbf    d2,DecrunchBlock    ; One time more than the initial d2
  625. EndOfLoop:
  626. _pp_DecrunchColor:
  627.     move.w    a1,$dff1a2        ; Set colour
  628.     cmp.l    a1,a2            ; Check if we have reached/passed 
  629.     bcs    LoopCheckCrunch        ;  lower limit...
  630.     rts                ; Yeah - chicken out!
  631.  
  632. myBitsTable:
  633.     dc.b    $09,$0a,$0b,$0b        ; Efficiency table
  634. ***********************************************************************
  635.  
  636. That's all for now - hope you don't mind...
  637. If you want to use the PowerPacker decrunching routine, I suggest instead
  638. using powerpacker.library if you are on the Amiga; if it is for a demo, I
  639. suggest using another cruncher, for instance CrunchMaster.  It has better
  640. compression ratios than PowerPacker.
  641.  
  642. Vishnu CRB           Feel free to e-mail me.
  643. steinarm@ifi.uio.no  "...all the modern inconveniences..." (Mark Twain)
  644.